// pages/drinkerInfo/drinkerInfo.js
import QRCode from '../../utils/weapp-qrcode.js'
import http from "../../utils/http.js"
const W = wx.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
const qrcodeWidth = 290 / rate;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    qrcodeShow: false,
    unfollowShow: false,
    delAllShow: false,
    delPersonShow: false,
    delUserId:0,
    isHouseholder: 0,
    memberList:[],
    followList:[],
    unFollowIdx: 0,
    customBar: 0,
    screenScale: 1,
    qrcodeWidth,
    qrcode: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      customBar: app.globalData.CustomBar,
      screenScale: app.globalData.screenScale
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList(1)
    this.getList(2)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if(res.from == "button") {
      let userId = res.target.dataset.userid
      return {
        title: app.globalData.shareTitle,
        path: '/pages/index/index?userid=' + userId + "&type=1" // 分享
      }
      return
    }
    return {
      title: app.globalData.shareTitle,
      path: '/pages/index/index'
    }
  },
  getList(type){
    let params = {
      type // 	1 家庭成员 2 好友成员
    }
    http.request("/drinkUsers/members",params)
    .then(res=>{
      if(type == 1) {
        this.setData({
          isHouseholder: res.data.isHouseholder,
          memberList: res.data.records
        })
      }else{
        this.setData({
          followList: res.data.records
        })
      }
    })
    .catch(res=>{

    })
  },
  select(e) {
    let active = e.currentTarget.dataset.id
    this.setData({
      active
    })
  },
  naviToAddFamily(e){
    let type = e.currentTarget.dataset.type
    let id = e.currentTarget.dataset.id
    let islogin = e.currentTarget.dataset.islogin
    wx.navigateTo({
      url: '/pages/addFamily/addFamily?islogin='+islogin+'&type='+type+'&userId=' + id , // 0 添加 1 编辑
    })
  },
  naviToHistory(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/history/history?userId=' + id,
    })
  },
  openQrcodeShow(e){
    let id = e.currentTarget.dataset.id
    
    this.setData({
      qrcodeShow: true
    })

    this.createQrcode(id+'')
  },
  openDelAll(e){
    let id = e.currentTarget.dataset.userid
    this.setData({
      delAllShow: true,
      delUserId:id
    })
  },
  openDelPerson(e){
    let id = e.currentTarget.dataset.userid
    this.setData({
      delPersonShow: true,
      delUserId:id
    })
  },
  delPerson(){
    let id = this.data.delUserId
    let params = {
      userId: id,
      admin:0
    }
    http.request("/home/deleteFamilyByUserId", params).then(res=>{
      this.setData({
        delPersonShow: false,
        delUserId:0
      });
      this.onShow();
    })
    .catch(res=>{
    })
  },
  delAll(){
    let id = this.data.delUserId
    let params = {
      userId: id,
      admin:1
    }
    http.request("/home/deleteFamilyByUserId", params).then(res=>{
      this.setData({
        delAllShow: false,
        delUserId:0
      });
      this.onShow();
    })
    .catch(res=>{
      this.setData({
        delAllShow: false,
        delUserId:0
      });
    })
  },
  onDelClose(e){
    this.setData({
      delAllShow: false,
      delPersonShow: false,
      delUserId:0
    })
  },
  scanCode(){
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        let params = {
          friendId: res.result
        }
        http.request("/drinkUsers/scanFriend",params)
        .then(res=>{
          wx.showToast({
            title: res.massage,
            icon: 'none'
          })
        })
        .catch(res=>{

        })
      }
    })
  },
  onClose(){
    this.setData({
      qrcodeShow: false,
      unfollowShow: false
    })
  },
  openUnfollowShow(e){
    let unFollowIdx = e.currentTarget.dataset.idx
    this.setData({
      unfollowShow: true,
      unFollowIdx
    })
  },
  unfollow(){
    let followList = this.data.followList
    let params = {
      friendId: this.data.followList[this.data.unFollowIdx].userId
    }
    http.request("/drinkUsers/unFollow", params)
    followList.splice(this.data.unFollowIdx,1)
    this.setData({
      unfollowShow: false,
      followList
    })
  },
  createQrcode(text) {
    this.data.qrcode = new QRCode('qrcode', {
      // usingIn: this,
      text: text,
      // image: '/images/bg.jpg',
      // padding: 12,
      width: qrcodeWidth,
      height: qrcodeWidth,
      // colorDark: "#1CA4FC",
      // colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
      callback: (res) => {
        // 生成二维码的临时文件
        console.log(res.path)
      }
    });
  },
})